﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using TCS.DBUtility;//请先添加引用
namespace TCS.DAL
{
    /// <summary>
    /// 数据访问类Gsp_SalesInfo。
    /// </summary>
    public class Gsp_SalesInfo
    {
        public Gsp_SalesInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("CGID", "Gsp_SalesInfo");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int CGID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Gsp_SalesInfo");
            strSql.Append(" where CGID=@CGID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CGID", SqlDbType.Int,4)};
            parameters[0].Value = CGID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(TCS.Model.Gsp_SalesInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Gsp_SalesInfo(");
            strSql.Append("MemberID,DrugID,BatchNumber,Period,PurchaseUnit,SalesCount,SalesTime,Managor)");
            strSql.Append(" values (");
            strSql.Append("@MemberID,@DrugID,@BatchNumber,@Period,@PurchaseUnit,@SalesCount,@SalesTime,@Managor)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MemberID", SqlDbType.Int,4),
					new SqlParameter("@DrugID", SqlDbType.Int,4),
					new SqlParameter("@BatchNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@Period", SqlDbType.VarChar,30),
					new SqlParameter("@PurchaseUnit", SqlDbType.NVarChar,50),
					new SqlParameter("@SalesCount", SqlDbType.Int,4),
					new SqlParameter("@SalesTime", SqlDbType.VarChar,30),
					new SqlParameter("@Managor", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.MemberID;
            parameters[1].Value = model.DrugID;
            parameters[2].Value = model.BatchNumber;
            parameters[3].Value = model.Period;
            parameters[4].Value = model.PurchaseUnit;
            parameters[5].Value = model.SalesCount;
            parameters[6].Value = model.SalesTime;
            parameters[7].Value = model.Managor;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(TCS.Model.Gsp_SalesInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Gsp_SalesInfo set ");
            strSql.Append("MemberID=@MemberID,");
            strSql.Append("DrugID=@DrugID,");
            strSql.Append("BatchNumber=@BatchNumber,");
            strSql.Append("Period=@Period,");
            strSql.Append("PurchaseUnit=@PurchaseUnit,");
            strSql.Append("SalesCount=@SalesCount,");
            strSql.Append("SalesTime=@SalesTime,");
            strSql.Append("Managor=@Managor");
            strSql.Append(" where CGID=@CGID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CGID", SqlDbType.Int,4),
					new SqlParameter("@MemberID", SqlDbType.Int,4),
					new SqlParameter("@DrugID", SqlDbType.Int,4),
					new SqlParameter("@BatchNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@Period", SqlDbType.VarChar,30),
					new SqlParameter("@PurchaseUnit", SqlDbType.NVarChar,50),
					new SqlParameter("@SalesCount", SqlDbType.Int,4),
					new SqlParameter("@SalesTime", SqlDbType.VarChar,30),
					new SqlParameter("@Managor", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.CGID;
            parameters[1].Value = model.MemberID;
            parameters[2].Value = model.DrugID;
            parameters[3].Value = model.BatchNumber;
            parameters[4].Value = model.Period;
            parameters[5].Value = model.PurchaseUnit;
            parameters[6].Value = model.SalesCount;
            parameters[7].Value = model.SalesTime;
            parameters[8].Value = model.Managor;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
       
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int CGID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Gsp_SalesInfo ");
            strSql.Append(" where CGID=@CGID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CGID", SqlDbType.Int,4)};
            parameters[0].Value = CGID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TCS.Model.Gsp_SalesInfo GetModel(int CGID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 CGID,MemberID,DrugID,BatchNumber,Period,PurchaseUnit,SalesCount,SalesTime,Managor from Gsp_SalesInfo ");
            strSql.Append(" where CGID=@CGID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CGID", SqlDbType.Int,4)};
            parameters[0].Value = CGID;

            TCS.Model.Gsp_SalesInfo model = new TCS.Model.Gsp_SalesInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CGID"].ToString() != "")
                {
                    model.CGID = int.Parse(ds.Tables[0].Rows[0]["CGID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MemberID"].ToString() != "")
                {
                    model.MemberID = int.Parse(ds.Tables[0].Rows[0]["MemberID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DrugID"].ToString() != "")
                {
                    model.DrugID = int.Parse(ds.Tables[0].Rows[0]["DrugID"].ToString());
                }
                model.BatchNumber = ds.Tables[0].Rows[0]["BatchNumber"].ToString();
                model.Period = ds.Tables[0].Rows[0]["Period"].ToString();
                model.PurchaseUnit = ds.Tables[0].Rows[0]["PurchaseUnit"].ToString();
                if (ds.Tables[0].Rows[0]["SalesCount"].ToString() != "")
                {
                    model.SalesCount = int.Parse(ds.Tables[0].Rows[0]["SalesCount"].ToString());
                }
                model.SalesTime = ds.Tables[0].Rows[0]["SalesTime"].ToString();
                model.Managor = ds.Tables[0].Rows[0]["Managor"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CGID,MemberID,DrugID,BatchNumber,Period,PurchaseUnit,SalesCount,SalesTime,Managor ");
            strSql.Append(" FROM Gsp_SalesInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" CGID,MemberID,DrugID,BatchNumber,Period,PurchaseUnit,SalesCount,SalesTime,Managor ");
            strSql.Append(" FROM Gsp_SalesInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Gsp_SalesInfo";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/
        public DataSet GetStatisticsList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select enname,comname,productname,sum(SalesCount) as c ");
            strSql.Append(" from View_SalesInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" group by enname,comname,productname order by enname, c desc");
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  成员方法
    }
}

